<script>
import { mapActions, mapMutations } from 'vuex';
//#ifdef MP-WEIXIN
import logger from 'utils/log.js';
//#endif
//#ifdef H5
import { getUrlparams } from './utils/tools.js';
import { wxAuthUrl } from './config/wxAuthUrl.js';
//#endif
export default {
	onLaunch: function() {
		let self = this;
		uni.showLoading({
			title: '初始化中',
			mask: true
		});
		let sysInfo = uni.getSystemInfoSync();
		console.log(sysInfo);
		self.setSysInfo(sysInfo);
		//#ifdef MP-WEIXIN
		console.log('App Launch');
		const updateManager = uni.getUpdateManager();

		updateManager.onCheckForUpdate(function(res) {
			// 请求完新版本信息的回调中
			console.log(res.hasUpdate);
		});

		updateManager.onUpdateReady(function(res) {
			uni.showModal({
				title: '更新提示',
				content: '新版本已经准备好，是否重启应用？',
				success(res) {
					if (res.confirm) {
						// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
						uni.clearStorageSync();
						let fileManager = wx.getFileSystemManager();
						fileManager.rmdir({
							dirPath: `${wx.env.USER_DATA_PATH}/agentCode`,
							recursive: true,
							complete(res) {
								console.log(res);
								updateManager.applyUpdate();
							}
						});
					}
				}
			});
		});

		updateManager.onUpdateFailed(function(res) {
			// 新的版本下载失败
		});
		// uni.clearStorageSync();
		let openId = this.$store.state.user.userInfo.openId;
		if (openId) {
			uni.checkSession({
				success() {
					uni.hideLoading();
					uni.$emit('getedOpenId');
					self.setAppReady();
				},
				fail() {
					uni.login({
						success(e) {
							console.log('login', e);
							self.getUserInfoCallback(e);
						},
						fail(e) {
							uni.hideLoading();
							console.log('loginFail', e);
						}
					});
				}
			});
		} else {
			uni.login({
				success(e) {
					console.log('login', e);
					self.getUserInfoCallback(e);
				},
				fail(e) {
					uni.hideLoading();
					console.log('loginFail', e);
				}
			});
		}
		//#endif

		//#ifdef H5
		let code = getUrlparams('code');
		let openId = this.$store.state.user.userInfo.openId;
		if (!openId) {
			if (code) {
				uni.hideLoading();
			} else {
				uni.hideLoading();
				let url = wxAuthUrl(encodeURIComponent(location.href));
				location.href = url;
			}
		} else {
			uni.hideLoading();
		}

		//#endif
	},
	onShow: function() {
		let curPage = getCurrentPages();
		console.log('curPage', curPage);
		// console.log('App Show',this.$store.state.user.userInfo.openid);
		// let self = this;
		// if (!this.$store.state.user.userInfo.openid) {
		// 	uni.login({
		// 		success(e) {
		// 			console.log('login', e);
		// 			self.code2session({ code: e.code })
		// 				.then(res1 => {
		// 					console.log(res1);
		// 					if (res1 && res1.code == '000000') {
		// 						self.setUserState({
		// 							...self.$store.state.user.userInfo,
		// 							openId: res1.openId
		// 						});
		// 						uni.$emit('getedOpenId');
		// 					}
		// 					uni.hideLoading();
		// 				})
		// 				.catch(err => {
		// 					uni.hideLoading();
		// 				});
		// 		},
		// 		fail(e) {
		// 			uni.hideLoading();
		// 			console.log('loginFail', e);
		// 		}
		// 	});
		// }
	},
	onHide: function() {
		console.log('App Hide');
	},
	methods: {
		...mapActions({
			code2session: 'mp/code2session'
		}),
		...mapMutations({
			setUserState: 'user/setUserState',
			setSysInfo: 'sys/setSysInfo',
			setAppReady: 'setAppReady'
		}),
		getUserInfoCallback(res) {
			this.code2session({ code: res.code })
				.then(res1 => {
					console.log(res1);
					if (res1 && res1.code == '000000') {
						this.setUserState({
							openId: res1.openId
						});
						uni.$emit('getedOpenId');
						this.setAppReady();
					}
					uni.hideLoading();
				})
				.catch(err => {
					uni.hideLoading();
				});
		}
	}
};
</script>
<style lang="scss">
@import 'uview-ui/index.scss';
</style>
<style lang="less">
@import './common/uni.css';
@import './common/color-ui.css';
@import './common/icon.css';
@import './common/common.less';
@import './common/animation.css';
</style>
